<?php
namespace app\api\controller;
use \think\Db;
class Wx extends \think\Controller
{
    public $appid = 'wxfdfc7b745bf64a1b';
    public $secret = 'afe901254efd8115d015a5bae6e1f301';
    public function checkToken(){
        //返回echostr
        $echostr = $_GET['echostr'];
        if($echostr)
        {
            echo $echostr;
            exit;
        }
    }
//生成二维码
    public function index(){
        $redirect_url = urlencode('http://' . $_SERVER['SERVER_NAME'] . '/api/Wx/user');
//        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid."&redirect_uri=" . $redirect_url . "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appid.'&redirect_uri=http%3a%2f%2fdraw.lwz123.xin%2fapi%2fWx%2fuser&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
        $file = 'http://' . $_SERVER['SERVER_NAME'] . '/' . Qrcode('code',$url);
        $this->assign('file' , $file);
        return $this->fetch();
    }
    public function user(){
        $data = $this->request->param();
        $code = $data['code'];

        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appid.'&secret='.$this->secret.'&code='.$code.'&grant_type=authorization_code';

        $access = json_decode(curl_get($url), true );
        if($access['openid']){
            $r = Db('member')->where(['openid'=>$access['openid']])->field('id,nickname,mobile,wx_code')->find();

            if($r){  //有注册完成
            	if($r['mobile'] || $r['wx_code']){
            		session('user_id',$r['user_id'],3600*2);  //session保存2小时
                    echo '有信息,直接登录';
                	header('location:'.url('api/Index/index'));
                	die;
            	}else{
                    $this->assign('id',$r['id']);
                    $this->assign('nickname',$r['nickname']);
                    echo ' 补充信息';
                    return $this->fetch();
                    die;
                }
                
            }
            $userInfo = json_decode(curl_get('https://api.weixin.qq.com/sns/userinfo?access_token='.$access['access_token'].'&openid='.$access['openid'].'&lang=zh_CN'),true);
            if($userInfo['nickname']){
                $arr['nickname'] = $userInfo['nickname'];
                $arr['openid'] = $userInfo['openid'];
                $arr['head_img'] = $userInfo['headimgurl'];
                //$data['last_login'] = time();
                //$data['last_login_ip'] = getIP();
                $memberModel = new \app\api\model\Member;
                $id = $memberModel->allowField(true)->insertGetId($arr);
                if($id){
                    $this->assign('id',$id);
                    $this->assign('nickname',$userInfo['nickname']);
                    echo '新用户';
                    return $this->fetch('user');
                }else{
                    echo '111';die;
                    print_r($data);
                }
            }else{
                echo '222';die;
            }
        }else{
            echo 333;die;
        }

    }
    public function login(){
        $data = $this->request->param();
        $user_id = $data['user_id'];
        unset($data['user_id']);
        $res = Db('member')->where(['id'=>$user_id])->update($data);
        if($res){
            session('user_id',$user_id,3600*2);  //session保存2小时
            return json(['code'=>1,'msg'=>'恭喜你注册成功']);
        }else{
            return json(['code'=>0,'msg'=>'注册失败']);
        }
    }
    public function notice(){
        $this->assign('msg',I('msg'));
        $this->display();
    }
}